package com.lightman.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureTest {

    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws Exception {

        System.out.println("---- main ---- start..........");
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程: " + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果 " + i);
//        }, executor);

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程: " + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果 " + i);
//            return i;
//        }, executor).whenComplete((result, exception) -> {
//            // 虽然可以得到异常现信息,但是没法修改返回数据
//            System.out.println("异步任务完成了....结果是:" + result + "---异常是----:" + exception);
//        }).exceptionally(throwable -> {
//            // 可以感知异常,同时修改默认值
//            return 10;
//        });


//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程: " + Thread.currentThread().getId());
//            int i = 10 / 4;
//            System.out.println("运行结果 " + i);
//            return i;
//        }, executor).handle((result, exception) -> {
//            if (result != null) {
//                return result * 2;
//            }
//            if (exception != null) {
//                return 0;
//            }
//            return 0;
//        });


        /**
         * 线程串行化
         *  1): thenRun : 不能获取到上一步的执行结果
         */
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程: " + Thread.currentThread().getId());
//            int i = 10 / 4;
//            System.out.println("运行结果 " + i);
//            return i;
//        }, executor).thenApplyAsync((result) -> {
//            System.out.println("任务2启动了........" + result);
//
//            return "hello" + result;
//        }, executor);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询商品的图片信息");

            return "hello.jpg";
        }, executor);


        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品属性");
            return "黑色+256G";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品介绍");
            return "华为";
        }, executor);

//        future01.runAfterBothAsync(future02, () -> {
//            System.out.println("003");
//        }, executor);
//
//
//        future01.thenAcceptBothAsync(future02, (f1, f2) -> {
//            System.out.println("003------" + f1+"---"+ f2);
//
//        }, executor);

//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + ":" + f2 + " ->" + "haha";
//        }, executor);


//        future01.runAfterEitherAsync(future02, () -> {
//            System.out.println("003..... 之前的结果:");
//        }, executor);

//        futureImg.get();
//        futureAttr.get();
//        futureDesc.get();

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        anyOf.get();// 等待所有结果完成

        System.out.println("--------------"+ anyOf.get());

        System.out.println("---- main ---- end........");

        executor.shutdown();
    }
}
